package com.account.manager.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.account.manager.common.response.GenericResponse;
import com.account.manager.common.response.ListResponse;
import com.account.manager.common.response.ResponseBase;
import com.account.manager.service.AccountService;
import com.account.manager.vo.Account;

@RestController
public class UserController {
	
	private Logger LOG=LoggerFactory.getLogger(UserController.class);
	
	@Resource
	private AccountService accountService;
	
	/**
	 * 查询账户
	 * @param account  //查询信息
	 * @param startTime //开始时间
	 * @param endTime //结束时间
	 * @return
	 */
	@RequestMapping(value = "/api/query")
	@ResponseBody
	public ListResponse<Account> queryUser(Account account, Long startTime,
			Long endTime) {
		
		
		List<Account> list = accountService.findUserList(account,startTime, endTime);
		return new ListResponse<Account>(list);
	}
	
	/**
	 * 更新用户信息，账户编码不能为空
	 * @param account
	 * @return
	 */
	@RequestMapping(value = "/api/update")
	@ResponseBody
	public GenericResponse updateUser(Account account) {

		GenericResponse genericResponse = new GenericResponse();

		if (StringUtils.isEmpty(account.getCode())) {
			genericResponse.setCode(503);
			genericResponse.setMsg("account code cannot be null");
			return genericResponse;
		}

		accountService.updateAccountInfo(account.getCode(), account);
		return genericResponse;
	}
	
	/**
	 * 删除账户
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/api/delete", method = RequestMethod.GET)
	@ResponseBody
	public GenericResponse deleteUser(String code) {

		GenericResponse genericResponse = new GenericResponse();

		if (StringUtils.isEmpty(code)) {
			genericResponse.setCode(503);
			genericResponse.setMsg("account code cannot be null");
			return genericResponse;
		}

		accountService.deleteAccount(code);

		return genericResponse;
	}
	
	/**
	 * 添加账户
	 * @param account
	 * @return
	 */
	@RequestMapping(value = "/api/add")
	@ResponseBody
	public ResponseBase addUser(Account account) {
		accountService.insertAccount(account);
		return new ResponseBase();
	}
	
	
	/**
	 * 禁用用户账号，用户编码不能为空
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/api/forbidden", method = RequestMethod.GET)
	@ResponseBody
	public GenericResponse addUser(String code) {
		
		GenericResponse genericResponse = new GenericResponse();
		
		if (StringUtils.isEmpty(code)) {
			genericResponse.setCode(503);
			genericResponse.setMsg("account code cannot be null");
			return genericResponse;
		}
		
		Account account=new Account();
		account.setStat(2);  //禁用账号
		
		accountService.updateAccountInfo(code, account);
		
		return genericResponse;
	}
	
	
	/**
	 * 启用账号，编码不能为空
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/api/startUsing", method = RequestMethod.GET)
	@ResponseBody
	public GenericResponse startUsing(String code) {
		GenericResponse genericResponse = new GenericResponse();

		if (StringUtils.isEmpty(code)) {
			genericResponse.setCode(503);
			genericResponse.setMsg("account code cannot be null");
			return genericResponse;
		}
		
		Account account=new Account();
		account.setStat(1); //启用账号
		
		accountService.updateAccountInfo(code, account);
		
		return genericResponse;
	}
	
	/**
	 * 更新登录时间，编码不能为空
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/api/updateLoginTime", method = RequestMethod.GET)
	@ResponseBody
	public GenericResponse updateLoginTime(String code) {
		GenericResponse genericResponse = new GenericResponse();
		if (StringUtils.isEmpty(code)) {
			genericResponse.setCode(503);
			genericResponse.setMsg("account code cannot be null");
			return genericResponse;
		}
		
		Long currentTime=System.currentTimeMillis();
		
		Account account=new Account();
		account.setLastTime(new Timestamp(currentTime));
		
		accountService.updateAccountInfo(code, account);
		
		return genericResponse;
	}

}